package engine.Astar
{
	import flash.geom.Point;
	import flash.utils.Dictionary;

	/**
	 *地图格子数据
	 * @author zane
	 */
	public class MapGrid
	{
		/**
		 *
		 * @default
		 */
		public var nodes:Dictionary;

		private var nodesPool:Vector.<Node>;

		/**
		 *
		 * @param maps
		 */
		public function MapGrid(maps:Dictionary)
		{
			init();

			for each (var node:Node in nodes)
			{
				nodesPool.push(node);
			}

			for each (var cell:* in maps)
			{
				var node:Node=getNodeFormPool();
				node.reset();
				node.x=cell.x;
				node.y=cell.z;
				if (cell.run == 0 || cell.empty == 0)
				{
					node.walkAble=false;
				}
				else
				{
					node.walkAble=true;
				}
				nodes[cell.indexKey]=node;

			}

		}

		private function init():void
		{
			nodes=new Dictionary();

			if (!nodesPool)
				nodesPool=new Vector.<Node>;
		}

		private function getNodeFormPool():Node
		{
			return nodesPool.shift() || new Node();
		}

		/**
		 *获取单个节点
		 * @param pt
		 * @return
		 *
		 */
		public function getNodeByPoint(pt:Point):Node
		{
			return nodes[wrapperKey(pt.x, pt.y)] as Node;
		}


		/**
		 *查找周围的格子
		 * @param startPoint
		 * @param endPoint
		 * @return
		 */
		public function cycleCheck(startPoint:*, endPoint:*):Node
		{
			if (startPoint == null || endPoint == null)
				return null;

			var node:Node=nodes[endPoint.key] as Node;

			if (!node || !node.walkAble)
			{

				var px:int=endPoint.x;
				var py:int=endPoint.z;

				var nearPath:Vector.<Node>=new Vector.<Node>;
				var eightNodes:Vector.<Node>=new Vector.<Node>;

				for (var index:int=1; index < 16; index++)
				{
					var node1:Node=nodes[wrapperKey(px - index, py)] as Node; //上
					var node2:Node=nodes[wrapperKey(px - index, py + index)] as Node; //右上
					var node3:Node=nodes[wrapperKey(px, py + index)] as Node; //右
					var node4:Node=nodes[wrapperKey(px + index, py + index)] as Node //右下
					var node5:Node=nodes[wrapperKey(px, py + index)] as Node; //下
					var node6:Node=nodes[wrapperKey(px - index, py + index)] as Node; //左下
					var node7:Node=nodes[wrapperKey(px - index, py)] as Node; //左
					var node8:Node=nodes[wrapperKey(px - index, py - index)] as Node; //左上


					eightNodes.concat(node1, node2, node3, node4, node5, node6, node7, node8);

					while (eightNodes.length > 0)
					{
						var temNode:Node=eightNodes.pop();

						if (hasPass(temNode))
						{
							nearPath.push(temNode);
						}
					}
					if (nearPath.length > 0)
					{
						nearPath.sortOn('dis', Array.NUMERIC)
						return nearPath[0]

					}

					if (index > 16)
					{
						return null;
					}

				}

			}
			return node
		}

		private function hasPass(node:Node):Boolean
		{
			if (node && node.walkAble)
			{
				return true;
			}

			false
		}

		private function wrapperKey(x:int, y:int):String
		{
			return x + "|0|" + y;
		}

		private function unload():void
		{

		}


	}
}